package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Examination;
import com.itheima.service.ExaminationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * []
 *
 * @author : [itnan]
 * @version : [v1.0]
 * @createTime : [2025/2/14 20:44]
 */

@RestController
@RequestMapping("/userExamination")
public class ExaminationController {

    @Reference
    private ExaminationService examinationService;

    @Autowired
    private JedisPool jedisPool;

    @GetMapping("/report")
    public Result getExaminationReport(HttpServletRequest request) {
        Jedis jedis = null;
        try {
            // 获取cookie
            String cookieHeader = request.getHeader("Cookie");
            if (cookieHeader != null) {
                String[] cookies = cookieHeader.split("; ");
                for (String cookie : cookies) {
                    if (cookie.startsWith("member_login_telephone=")) {
                        String telephone = cookie.split("=")[1];
                        //先看redis中有没有数据
                        jedis = jedisPool.getResource();
                        String examinationList = jedis.get("examination:" + telephone);
                        if(examinationList != null && !"".equals(examinationList)){
                            List<Examination> examinations = JSON.parseArray(examinationList, Examination.class);
                            return new Result(true,"", examinations);
                        }
                        // 使用手机号查询检查报告
                        if (examinationList == null || "".equals(examinationList)) {
                            jedis.setex("examination:" + telephone, 60 * 60 * 24 * 7, JSON.toJSONString(examinationService.getExaminationReportByTelephone(telephone)));
                        }
                        return new Result(true,"", examinationService.getExaminationReportByTelephone(telephone));
                    }
                }
            }
            // 如果没有找到手机号，返回错误信息
            return new Result(false, MessageConstant.GET_EXAMINATION_REPORT_FAIL);
        } catch (Exception e) {
            return new Result(false, MessageConstant.GET_EXAMINATION_REPORT_FAIL);
        } finally {
            // 关闭Jedis连接
            if(jedis != null){
                jedis.close();
            }
        }
    }

    @GetMapping("/findById/{id}")
    public Result findById(@PathVariable("id") Integer id) {
        Examination examination = examinationService.findExaminationById(id);
        if (examination != null) {
            return new Result(true, MessageConstant.QUERY_EXAMINATION_SUCCESS, examination);
        }
        return new Result(false, MessageConstant.QUERY_EXAMINATION_FAIL);
    }
}
